﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HR.Appointments.Service.Tools;
using HR.Appointments.Service.DataAccessLayer;
using HR.Appointments.Service.DataAccessLayer.DLinq;

namespace HR.Appointments.Service
{
    /// <summary>
    /// Singleton class to hold reference to Data Access Layer Objects
    /// </summary>
    public class DAO
    {
        #region Private Members
        private bool _initialized = false;
        #endregion

        #region Private static Members
        private static DAO _instance;
        private static object _syncObject = new object();
        #endregion

        #region Static Instance Properties
        /// <summary>
        /// Instance to DAO Objects
        /// </summary>
        public static DAO Instance
        {
            get
            {
                lock (_syncObject)
                {
                    if (_instance == null)
                        _instance = new DAO();
                    return _instance;
                }
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Load all DLinq objects.
        /// </summary>
        public void Initialize()
        {
            if (!_initialized)
            {
                Registry.RegisterDAO(new OpenHoursDAO(), typeof(IOpenHoursDAO));
                Registry.RegisterDAO(new ReservedTimeDAO(), typeof(IReservedTimeDAO));
                Registry.RegisterDAO(new OrderDAO(), typeof(IOrderDAO));
                Registry.RegisterDAO(new CompanyDAO(), typeof(ICompanyDAO));
                Registry.RegisterDAO(new ActivityDAO(), typeof(IActivityDAO));
                Registry.RegisterDAO(new WorkstationDAO(), typeof(IWorkstationDAO));
            }
        }

        /// <summary>
        /// Unload the DLinq objects
        /// </summary>
        public void DeInitialize()
        {
            Registry.DeRegisterDAO(typeof(IOpenHoursDAO));
            Registry.DeRegisterDAO(typeof(IReservedTimeDAO));
            Registry.DeRegisterDAO(typeof(IOrderDAO));
            Registry.DeRegisterDAO(typeof(ICompanyDAO));
            Registry.DeRegisterDAO(typeof(IActivityDAO));
            Registry.DeRegisterDAO(typeof(IWorkstationDAO));

            _initialized = false;
        }
        
        /// <summary>
        /// Gets the open hour data access object
        /// </summary>
        public IOpenHoursDAO OpenHours
        {
            get
            {
                return (IOpenHoursDAO)Registry.GetDAO(typeof(IOpenHoursDAO));
            }
        }

        /// <summary>
        /// Gets the reserved data access object
        /// </summary>
        public IReservedTimeDAO ReservedTime
        {
            get
            {
                return (IReservedTimeDAO)Registry.GetDAO(typeof(IReservedTimeDAO));
            }
        }

        /// <summary>
        /// Gets the order data access object
        /// </summary>
        public IOrderDAO Order
        {
            get
            {
                return (IOrderDAO)Registry.GetDAO(typeof(IOrderDAO));
            }
        }

        /// <summary>
        /// Gets the activity data access object
        /// </summary>
        public IActivityDAO Activity
        {
            get
            {
                return (IActivityDAO)Registry.GetDAO(typeof(IActivityDAO));
            }
        }

        /// <summary>
        /// Gets the workstation data access object
        /// </summary>
        public IWorkstationDAO Workstation
        {
            get
            {
                return (IWorkstationDAO)Registry.GetDAO(typeof(IWorkstationDAO));
            }
        }

        /// <summary>
        /// Gets the copmany data access object
        /// </summary>
        public ICompanyDAO Company
        {
            get
            {
                return (ICompanyDAO)Registry.GetDAO(typeof(ICompanyDAO));
            }
        }
        #endregion
    }
}
